#!/bin/bash
clear
echo -e "小战云原创脚本官网：https://idc.sanxiaozhan.org/"
echo -e "小战云 为您提供稳定可靠的云服务 国内服务器40元起，海外服务器20元起"
echo -e "
执行该脚本前
请先创建一个数据库 版本5.7 8.0 都可以
数据库名和用户名必须设置为：umami
密码必须设置为：umami5ahsbjop"
sleep 5s

# 检测并安装/更新 Docker
if ! command -v docker &> /dev/null; then
	    echo "Docker 未安装。开始安装 Docker..."
	        if [ -f /etc/debian_version ]; then
			        sudo apt update
				        sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
					        curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
						        sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
							        sudo apt update
								        sudo apt install -y docker-ce
									    elif [ -f /etc/centos-release ]; then
										            sudo yum install -y yum-utils
											            sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
												            sudo yum install -y docker-ce docker-ce-cli containerd.io
													        else
															        echo "不支持的操作系统。请手动安装 Docker。"
																        exit 1
																	    fi
																	        sudo systemctl start docker
																		    sudo systemctl enable docker
																	    else
																		        echo "Docker 已安装。"
																			    read -p "是否更新到最新版？(y/n): " update_docker
																			        if [ "$update_docker" == "y" ]; then
																					        if [ -f /etc/debian_version ]; then
																							            sudo apt update
																								                sudo apt upgrade -y docker-ce
																										        elif [ -f /etc/centos-release ]; then
																												            sudo yum update -y docker-ce
																													            fi
																														        fi
fi

# 检测并安装/更新 Docker Compose
if ! command -v docker-compose &> /dev/null; then
	    echo "Docker Compose 未安装。开始安装 Docker Compose..."
	        if [ -f /etc/debian_version ]; then
			        sudo apt update
				        sudo apt install -y docker-compose
					    elif [ -f /etc/centos-release ]; then
						            sudo yum install -y epel-release
							            sudo yum install -y docker-compose
								        fi
								else
									    echo "Docker Compose 已安装。"
									        read -p "是否更新到最新版？(y/n): " update_compose
										    if [ "$update_compose" == "y" ]; then
											            if [ -f /etc/debian_version ]; then
													                sudo apt update
															            sudo apt upgrade -y docker-compose
																            elif [ -f /etc/centos-release ]; then
																		                sudo yum update -y docker-compose
																				        fi
																					    fi
fi

# 询问是否更换为镜像源
read -p "是否更换为镜像源？(y/n): " change_mirror
if [ "$change_mirror" == "y" ]; then
	    sudo mkdir -p /etc/docker
	        echo '{"registry-mirrors": ["http://dk.wsvg.cn"]}' | sudo tee /etc/docker/daemon.json
		    sudo systemctl restart docker
fi

# 下载 docker-compose.yml 文件
echo "下载 Umami 的 docker-compose.yml 文件..."
sudo curl -L "https://gitee.com/wwwwwddzafv/umami/raw/master/docker-compose.yml" -o /root/docker-compose.yml

# 启动 Umami 服务
echo "启动 Umami 服务..."
cd /root
sudo docker-compose up -d

# 输出完成信息
echo -e "\033[31m安装成功\033[0m"
echo -e "\033[31mIP:3000 即可访问\033[0m"
echo -e "\033[31m默认用户名：admin\033[0m"
echo -e "\033[31m密码：umami\033[0m"
echo -e "\033[31m请尽快修改\033[0m"
